package traditional.thread.communicate;

public class TraditionalTheadCommuniceTest {

	boolean isShouldBoss = false;

	public static void main(String[] args) {

		final TraditionalTheadCommuniceTest ttc = new TraditionalTheadCommuniceTest();

		// 整体循环50次
		for (int i = 0; i < 50; i++) {

			new Thread(new Runnable() {

				public void run() {
					// 大哥来搞100次
					ttc.main();

				}
			}).start();

			new Thread(new Runnable() {

				public void run() {
					// 小弟来搞10次
					ttc.sub();

				}
			}).start();

			System.out.println("主循环次数:" + i);

		}

	}

	synchronized void main() {
		while (isShouldBoss) {
			for (int i = 0; i < 100; i++) {
				System.out.println("main:" + i);
			}
			isShouldBoss = false;
			this.notify();
		}
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	synchronized void sub() {
		while (!isShouldBoss) {
			for (int i = 0; i < 10; i++) {
				System.out.println("sub:" + i);
			}
			isShouldBoss = true;
			this.notify();
		}
		try {
			this.wait();
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

}
